package com.example.music.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.music.App;
import com.example.music.R;
import com.example.music.entity.DataMusic;

import java.util.List;

public class HyListAdapter extends BaseAdapter {

    private List<DataMusic> musics;
    private int pn;
    private App app;

    public HyListAdapter(App app,List<DataMusic> musics, int pn) {
        this.musics = musics;
        this.pn = pn;
        this.app = app;
    }

    @Override
    public int getCount() {
        return musics.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        if(view==null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_close, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.name = view.findViewById(R.id.name);
            viewHolder.author = view.findViewById(R.id.author);
            viewHolder.image = view.findViewById(R.id.image_);
            viewHolder.close = view.findViewById(R.id.close);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.name.setText(musics.get(position).getName());
        viewHolder.author.setText(musics.get(position).getAuthor());
        if(position==pn){
            viewHolder.name.setTextColor(Color.parseColor("#0066FF"));
            viewHolder.author.setTextColor(Color.parseColor("#0066FF"));
            viewHolder.image.setVisibility(View.VISIBLE);
        }else {
            viewHolder.name.setTextColor(Color.BLACK);
            viewHolder.author.setTextColor(Color.BLACK);
            viewHolder.image.setVisibility(View.GONE);
        }
        viewHolder.close.setOnClickListener(v -> {
            app.remove(position);
            notifyDataSetChanged();
        });
        view.setOnClickListener(v->{
            app.setPn(position);
        });
        return view;
    }
    class ViewHolder{
        ImageView close;
        ImageView image;
        TextView name;
        TextView author;
    }
}
